﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Innova.Microfinanzas.BusinessEntities;
using MyCommerce.Business.GeneralParameters;
using MyCommerce.ManagementBase.Common.Base.BaseEntities;

namespace Innova.Microfinanzas.Windows.Common.Views.Common
{
    public partial class CustomerSearchControl : UserControl
    {
        public CustomerSearchControl()
        {
            InitializeComponent();
        }

        public ContextEntity Context
        {
            get;
            set;
        }

        public Customer ObjCustomerLoad
        {
            get;
            set;
        }

        public void SetCustomer(Customer objCustomer)        
        {
            ObjCustomerLoad = objCustomer;
            fieldCustomer.Text = objCustomer.FullName;
        }

        private void SearchCustomer()
        {
            if (!String.IsNullOrEmpty(fieldCustomer.Text.Trim()))
            {
                CustomerManager customerMgr = new CustomerManager();
                List<Customer> colCustomers = customerMgr.FindCustomerByNameOrIdentityCard(fieldCustomer.Text.Trim());
                if (colCustomers != null
                    && colCustomers.Count > 0)
                {
                    Customer objSelected = null;
                    if (colCustomers.Count == 1)
                        objSelected = colCustomers[0];
                    else
                        objSelected = CustomerSelectionList.ShowSelectedCustomerView(colCustomers);

                    if (objSelected != null)
                        SetCustomer(objSelected);
                }
                else
                {
                    Context.ShowInformationMessageBox("No se obtuvieron resultados en la búsqueda.", "Mensaje");
                }
            }
            else
            {
                Context.ShowInformationMessageBox("Debe ingresar datos para poder realizar la búsqueda.", "Mensaje");
            }
        }

        public void ClearControl()
        {
            ObjCustomerLoad = null;
            fieldCustomer.Text = "";
        }

        public void NewEditCustomer(Customer customerLoad)
        {
            Command commandLoad = Context.GetCommand("CustomerRegistration");
            if (commandLoad != null)
            {
                object vResult = Context.InvoqueCommandWithParams(commandLoad, customerLoad, true);
                if (vResult != null)
                {
                    long idPerson = Convert.ToInt64(vResult);
                    if (idPerson > 0)
                    {
                        CustomerManager customerMgr = new CustomerManager();
                        SetCustomer(customerMgr.GetCustomerById(idPerson));
                    }
                }
            }
        }

        private void fieldCustomer_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {
                SearchCustomer();
            }
            else if (e.KeyChar == (char)Keys.Back || e.KeyChar == (char)Keys.Delete)
            {
                if (ObjCustomerLoad != null)
                    ClearControl();
            }
            else if (ObjCustomerLoad != null)
                e.KeyChar = '\0';
        }

        private void fieldCustomer_EditorButtonClick(object sender, Infragistics.Win.UltraWinEditors.EditorButtonEventArgs e)
        {
            switch (e.Button.Key)
            {
                case "new":
                    NewEditCustomer(null);
                    break;
                case "edit":
                    if (ObjCustomerLoad != null)
                        NewEditCustomer(ObjCustomerLoad);
                    else
                        Context.ShowInformationMessageBox("No se tiene ningún cliente seleccionado en el control para poder editarlo.", "Mensaje");
                    break;
                case "clear":
                    ClearControl();
                    break;
            }
        }

        private void CustomerSearchControl_Load(object sender, EventArgs e)
        {
            if (!this.DesignMode)
            {
                Context = ContextEntity.FindContext(this);
            }
        }
    }
}
